// 导入 MySQL 模块
const util = require("../../common/util");
// 创建 MySQL 连接池
const pool = require("../../common/util");

class WarehouseDB {
    /**
     * @constructor
     * @private
     */
    constructor() {
        this.instance = null;
        this.pool = pool;
    }

    /**
     * 创建 WarehouseDB 对象
     * @returns {WarehouseDB} WarehouseDB 实例
     */
    static getInstance() {
        if (!this.instance) {
            this.instance = new WarehouseDB();
        }
        return this.instance;
    }
}

/**
 * 增加仓库
 * @param {Object} warehouse 仓库对象
 * @returns {Promise<Object>} 仓库对象
 */
WarehouseDB.prototype.add = async function (warehouse) {
    try {
      // 构造插入仓库的 SQL 语句
      const sql = `INSERT INTO warehouse (
        name, location, size, total_capacity, max_height, description, manager, active, equipment, security_level, storage_type
      ) VALUES (
        ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?
      )`;
  
      // 提取仓库数据作为参数
      const params = [
        warehouse.name,
        warehouse.location,
        warehouse.size,
        warehouse.totalCapacity,
        warehouse.maxHeight,
        warehouse.description,
        warehouse.manager,
        warehouse.active , 
        warehouse.equipment,
        warehouse.securityLevel,
        warehouse.storageType
      ];
  
      // 使用 query 函数执行 SQL 插入操作
      const result = await this.pool.query(sql, params);
  
      // 如果成功插入，返回插入的 ID（通常是第一个结果中的 insertId）
      return { id: result.insertId };
    } catch (err) {
      // 在出现错误时抛出异常
      util.err(err);
      throw err; // 重新抛出错误，以便调用者可以处理它
    }
  };

 /**
 * 更新仓库
 * @param {Object} warehouse 仓库对象
 * @returns {Promise<Object>} 更新后的仓库对象
 */
WarehouseDB.prototype.update = async function (warehouse) {
    try {
      // 构造更新仓库的 SQL 语句
      const sql = `UPDATE warehouse 
        SET 
          name = ?, 
          location = ?, 
          size = ?, 
          total_capacity = ?, 
          max_height = ?, 
          description = ?, 
          manager = ?, 
          active = ?, 
          equipment = ?, 
          security_level = ?, 
          storage_type = ?
        WHERE id = ?`;
  
      // 提取仓库数据作为参数
      const params = [
        warehouse.name,
        warehouse.location,
        warehouse.size,
        warehouse.totalCapacity,
        warehouse.maxHeight,
        warehouse.description,
        warehouse.manager,
        warehouse.active,
        warehouse.equipment,
        warehouse.securityLevel,
        warehouse.storageType,
        warehouse.id // 添加仓库ID作为 WHERE 子句的条件
      ];
  
      // 使用 query 函数执行 SQL 更新操作
      const result = await this.pool.query(sql, params);
  
      // 返回更新后的仓库对象
      return warehouse; // 返回原始仓库对象，因为结果通常只包含受影响的行数
    } catch (err) {
      // 在出现错误时抛出异常
      util.err(err);
      throw err; // 重新抛出错误，以便调用者可以处理它
    }
  };
  


/**
 * 获取指定ID的仓库详情
 * @param {Number} warehouseId 仓库ID
 * @returns {Promise<Object>} 仓库详情
 */
WarehouseDB.prototype.find = async function (warehouseId) {
    try {
      // 构造查询仓库详情的 SQL 语句
      let sql = `SELECT * FROM warehouse WHERE id = ?`;
      let params = [warehouseId];
  
      // 使用 query 函数执行 SQL 查询操作
      let result = await this.pool.query(sql, params);
      console.log(result);
      // 返回查询结果
      return result;
    } catch (err) {
      // 在出现错误时抛出异常
      throw err;
    }
  };

  /**
 * 获取所有的仓库详情
 * @param {Number} warehouseId 仓库ID
 * @returns {Promise<Object>} 仓库详情
 */
WarehouseDB.prototype.findAll = async function () {
    try {
      // 构造查询仓库详情的 SQL 语句
      let sql = `SELECT * FROM warehouse `;
      let params = [];
  
      // 使用 query 函数执行 SQL 查询操作
      let result = await this.pool.query(sql, params);
      console.log(result);
      // 返回查询结果
      return result;
    } catch (err) {
      // 在出现错误时抛出异常
      throw err;
    }
  };
  
  
/**
 * 获取指定ID的仓库详情
 * @param {Number} warehouseId 仓库ID
 * @returns {Promise<Object>} 仓库详情
 */
WarehouseDB.prototype.remove = async function (warehouseId) {
    try {
      // 构造查询仓库详情的 SQL 语句
      let sql = `DELETE FROM warehouse WHERE id = ?`;
      let params = [warehouseId];
  
      // 使用 query 函数执行 SQL 查询操作
      let result = await this.pool.query(sql, params);
      // 返回查询结果
      return result.affectedRows;
    } catch (err) {
      // 在出现错误时抛出异常
      throw err;
    }
  };
module.exports = WarehouseDB;